package com.airbat.prize.utils;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

public class PersonBgCellWriteHandler extends AbstractCellWriteHandler {

    int familyCount = 0;
    int criminal = 0;

    public PersonBgCellWriteHandler(int familyCount, int criminal) {
        this.familyCount = familyCount;
        this.criminal = criminal;
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        super.afterCellDispose(writeSheetHolder, writeTableHolder, cellDataList, cell, head, relativeRowIndex, isHead);

        writeSheetHolder.getSheet().setColumnWidth(0, 5 * 25);
        writeSheetHolder.getSheet().setColumnWidth(1, 100 * 25);
        writeSheetHolder.getSheet().setColumnWidth(2, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(3, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(4, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(5, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(6, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(7, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(8, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(9, 120 * 25);
        writeSheetHolder.getSheet().setColumnWidth(10, 5 * 25);

        int needCount = 0;
        {
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(1, 1, 1, 9));

            //被举报人基本信息
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(3, 7, 1, 1));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(4, 4, 7, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(5, 5, 3, 5));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(5, 5, 7, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(6, 6, 3, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(7, 7, 3, 9));

            //直系亲属信息
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(8 + needCount, 8 + needCount, 5, 7));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(8 + needCount, 8 + needCount, 8, 9));
            needCount++;
            if (familyCount > 0) {
                writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(8, 8 + familyCount, 1, 1));
                for (int i = 0; i < familyCount; i++) {
                    writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(8 + needCount, 8 + needCount, 5, 7));
                    writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(8 + needCount, 8 + needCount, 8, 9));
                    needCount++;
                }
                needCount--;
            }

            //前科情况
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + needCount, 9 + needCount, 3, 4));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + needCount, 9 + needCount, 5, 6));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + needCount, 9 + needCount, 7, 8));
            needCount++;
            if (criminal > 0) {
                writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + familyCount, 9 + familyCount + criminal, 1, 1));
                for (int i = 0; i < criminal; i++) {
                    writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + needCount, 9 + needCount, 3, 4));
                    writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + needCount, 9 + needCount, 5, 6));
                    writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(9 + needCount, 9 + needCount, 7, 8));
                    needCount++;
                }
                needCount--;
            }

            //背景联查
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(10 + needCount, 14 + needCount, 1, 1));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(10 + needCount, 10 + needCount, 2, 4));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(10 + needCount, 10 + needCount, 5, 6));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(10 + needCount, 10 + needCount, 7, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(11 + needCount, 11 + needCount, 2, 4));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(11 + needCount, 11 + needCount, 5, 6));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(11 + needCount, 11 + needCount, 7, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(12 + needCount, 12 + needCount, 2, 4));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(12 + needCount, 12 + needCount, 5, 6));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(12 + needCount, 12 + needCount, 7, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(13 + needCount, 13 + needCount, 2, 4));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(13 + needCount, 13 + needCount, 5, 6));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(13 + needCount, 13 + needCount, 7, 9));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(14 + needCount, 14 + needCount, 2, 4));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(14 + needCount, 14 + needCount, 5, 9));

            //附件信息
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(15 + needCount, 15 + needCount, 1, 9));

            //调查人、日期
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(16 + needCount, 16 + needCount, 6, 7));
            writeSheetHolder.getSheet().addMergedRegionUnsafe(new CellRangeAddress(16 + needCount, 16 + needCount, 8, 9));
        }

        if (cell.getRowIndex() >= 3 && cell.getColumnIndex() <= 9) {
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setFontName("思源宋体"); // 设置字体名称
            font.setFontHeightInPoints((short) 11);
            cellStyle.setFont(font);
            cellStyle.setWrapText(true);
            if (15 + needCount == cell.getRowIndex() || 16 + needCount == cell.getRowIndex()) {
                cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
            } else {
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
                cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            }
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
            cell.setCellStyle(cellStyle);
        } else {
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setFontName("思源宋体"); // 设置字体名称
            font.setFontHeightInPoints((short) 11);
            cellStyle.setFont(font);
            cellStyle.setBorderTop(BorderStyle.NONE);
            cellStyle.setBorderBottom(BorderStyle.NONE);
            cellStyle.setBorderLeft(BorderStyle.NONE);
            cellStyle.setBorderRight(BorderStyle.NONE);
            cell.setCellStyle(cellStyle);
        }
        if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 1) {
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setFontName("思源宋体"); // 设置字体名称
            font.setFontHeightInPoints((short) 16);
            font.setBold(true); // 设置字体加粗
            cellStyle.setFont(font);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cell.setCellStyle(cellStyle);
        }

        if (cell.getColumnIndex() == 0 || cell.getColumnIndex() == 10) {
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), 20 * 256);
        }
        if (relativeRowIndex == 0 || relativeRowIndex == 2) {
            writeSheetHolder.getSheet().getRow(relativeRowIndex).setHeightInPoints(5);
        } else if (relativeRowIndex == needCount + 15) {
            writeSheetHolder.getSheet().getRow(relativeRowIndex).setHeightInPoints(80);
        } else {
            writeSheetHolder.getSheet().getRow(relativeRowIndex).setHeightInPoints(25);
        }
    }

}
